/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CalStat.LR;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Anatomy
 */
public class Regression {
double values[];
public int t;
public static List<String>[] list = new List[20]; 
//int t2[]=new int[t];
//double sum[]=new double[t];
double LR;
     int nexttime;
    public Regression(double value[],int time,int nexttime) {
        this.t=time;
     this.nexttime=nexttime;
        this.values=value;
    }
   public double cal(){
        
       for(int i=0;i<t;i++){
          list[i]=new LinkedList<>();
          list[i].add(String.valueOf(i+1));
          list[i].add(String.valueOf(values[i]));
          list[i].add(String.valueOf(i+1));
          list[i].add(String.valueOf((i+1)*(i+1)));
          list[i].add(String.valueOf(values[i]*(i+1)));
         //  sum[i]+=values[i];
          // t2[i]=(i*i);
           
           
       }
          list[t]=new LinkedList<>();
          list[t].add(String.valueOf("SUM"));
          list[t].add(String.valueOf(this.sumvalues()));
          list[t].add(String.valueOf(this.sumt(0)));
          list[t].add(String.valueOf(this.sumt(1)));
          list[t].add(String.valueOf(this.sumt(2)));
          
          double b=(((t*V(list[t].get(4)))-(V(list[t].get(1))*V(list[t].get(2))))/((t*V(list[t].get(3)))-(V(list[t].get(2))*V(list[t].get(2)))));
          double a=((V(list[t].get(1))/t)-((b*V(list[t].get(2))))/t);
           System.out.println("b="+b);   
          System.out.println("a="+a); 
         
         double yt=a+(b*nexttime);
          System.out.println("yt="+yt); 
       return yt;
    }

   public static void main(String[]ar){
       double s[]={7,8,10,12,13,14,16,18,20};
       Regression rg=new Regression(s,8,10);
       rg.cal();
       for(int i=0;i<=rg.t;i++){
        //   for(int j=0;j<list[j].toArray().length;j++){
              System.out.println(list[i]); 
        //   }
           
       }
       
      
       
   }
   public double sumvalues(){
       double sumvalues=0;
   for(int i=0;i<t;i++){
        sumvalues+=values[i];
    }
   return sumvalues;
}
   public double V(String text){
      
   return Double.parseDouble(text);
}
public double sumt(int stat){
    
       double sumt=0;
  if(stat==0){
  for(int i=0;i<t;i++){
     sumt +=  Double.parseDouble(list[i].get(2));     
       }
  }else if(stat==1){
        for(int i=0;i<t;i++){
     sumt +=  Double.parseDouble(list[i].get(3));     
       }
           
  }else if(stat==2){
        for(int i=0;i<t;i++){
     sumt +=  Double.parseDouble(list[i].get(4));     
       }
           
  }
   return sumt;
}
    
    
}
